package com.ergwang.controller.mall;

import com.ergwang.Model.Result;
import com.ergwang.Model.User;
import com.ergwang.Model.admin.vo.AdminLoginVO;
import com.ergwang.Model.mall.bo.UserLoginBO;
import com.ergwang.service.admin.UserService;
import com.ergwang.service.admin.UserServiceImpl;
import com.ergwang.utils.HttpUtils;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author：ergwang
 * @Date: 2020/6/2
 * @Time: 11:58
 */
@WebServlet("/api/mall/user/*")
public class UserServlet extends HttpServlet {

    private UserService userService = new UserServiceImpl();
    private Gson gson = new Gson();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String action = requestURI.replace("/api/mall/user/", "");
        if ("login".equals(action)) {
            login(request, response);
        }else if ("signup".equals(action)){
            signup(request,response);
        }
    }

    /**
     * 注册
     * @param request
     * @param response
     */
    private void signup(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        User user = gson.fromJson(requestBody, User.class);
        User signup = userService.signup(user);
        if (signup != null){
            request.getSession().setAttribute("admin",signup);
            AdminLoginVO loginVO = new AdminLoginVO();
            loginVO.setToken(signup.getNickname());
            loginVO.setName(signup.getNickname());
            response.getWriter().println(gson.toJson(Result.ok(loginVO)));
        }else {
            response.getWriter().println(gson.toJson(Result.error(",用户名或密码错误")));
        }
    }

    /**
     * 前台的登陆
     * @param request
     * @param response
     */
    private void login(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        UserLoginBO userLoginBO = gson.fromJson(requestBody, UserLoginBO.class);
        User login = userService.login(userLoginBO);
        // 判断是否有获得到一个对象，作出响应
        if (login != null){
            request.getSession().setAttribute("admin",login);
            AdminLoginVO loginVO = new AdminLoginVO();
            loginVO.setToken(login.getNickname());
            loginVO.setName(login.getNickname());
            response.getWriter().println(gson.toJson(Result.ok(loginVO)));
        }else {
            response.getWriter().println(gson.toJson(Result.error(",用户名或密码错误")));
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String action = requestURI.replace("/api/mall/user/", "");
        if ("data".equals(action)) {
            login(request, response);
        }
    }
}
